<template>
  <div class="wrap" id="children">
    <el-dialog
      title="编辑子账号"
      :visible.sync="state"
      @close="close"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @open="open"
    >
      <el-form>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model.trim="username" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="账号" :label-width="formLabelWidth">
          <el-input
            autocomplete="new-password"
            v-model.trim="account"
            placeholder="请输入账号"
            type="text"
            @focus="changeType"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input
            autocomplete="new-password"
            v-model.trim="password"
            type="text"
            placeholder="请输入密码"
            @focus="changeType"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" :label-width="formLabelWidth">
          <el-input
            autocomplete="new-password"
            v-model.trim="passwordCheck"
            type="text"
            placeholder="请再次输入密码"
            @focus="changeType"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="close(1)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    childrenItemState: {
      type: Boolean,
      default: false
    },
    userData:{
       type: null,
      default: null
    }
  },
  data() {
    return {
      formLabelWidth: "120px",
      userId: "",
      username: "",
      account: "",
      password: "",
      passwordCheck: ""
    };
  },
  computed: {
    state: {
      get() {
        return this.childrenItemState;
      },
      set() {}
    }
  },
  methods: {
    close(v) {
      if (v) {
        if (!this.username) {
          this.$alert("请输入名称", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (!this.account) {
          this.$alert("请输入账号", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (!(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$/.test(this.account))) {
          this.$alert("账号必须由英文和数字组成,至少6位", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (this.account.length < 6) {
          this.$alert("账号不能少于6位", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (!this.password) {
          this.$alert("请输入密码", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (this.password.length < 6) {
          this.$alert("密码不能少于6位", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (this.password !== this.passwordCheck) {
          this.$alert("两次密码输入不一致", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        let data = {
          username:this.username,
          account:this.account,
          password:this.password,
          userId:this.userId
        }
        this.$emit("getitem",data);
      } else {
        this.$emit("getitem");
      }
    },
    changeType(e) {
      e.srcElement.type = "password";
    },
    open(){
      this.username = this.userData.username
      this.account = this.userData.account
      this.userId = this.userData.user_id
      this.password = ""
    },
  }
};
</script>
<style lang="scss" scoped>
.el-input {
  max-width: 240px;

}
.el-dialog {
  width: 30% !important;
}
</style>
<style lang="scss">
#children .el-dialog {
  width: 30% !important;
  min-width: 300px;
}
</style>